package com.xi.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xi.mapper.BorrowMapper;
import com.xi.pojo.Book;
import com.xi.pojo.Borrow;
import com.xi.pojo.BorrowQueryParam;
import com.xi.pojo.PageResult;
import com.xi.service.BorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BorrowServiceImpl implements BorrowService {

    @Autowired
    private BorrowMapper BorrowMapper;

    /*
    * 分页查询借阅记录
    * */
    @Override
    public PageResult page(BorrowQueryParam borrowQueryParam) {
        // 1.设置分页参数
        PageHelper.startPage(borrowQueryParam.getPage(), borrowQueryParam.getPageSize());

        // 2.执行查询
        List<Borrow> rows = BorrowMapper.list(borrowQueryParam);

        //将书籍为空的添加为未知图书
        for (Borrow borrow : rows) {
            if (borrow.getBookName() == null) {
                borrow.setBookName("未知图书");
            }
        }

        // 3.解析查询结果，并封装
        Page<Borrow> p = (Page<Borrow>) rows;
        return new PageResult<>(p.getTotal(), p.getResult());
    }

    /*
    * 根据id删除借阅信息
    * */
    @Override
    public void deleteById(Integer id) {
        BorrowMapper.deleteById(id);
    }

    /*
    * 获取得到总借阅数
    * */
    @Override
    public Integer count() {
        return BorrowMapper.count();
    }

    /*
    * 获取借阅列表中图书类型最多的图书类型
    * */
    @Override
    public Object mostClassify() {
        return BorrowMapper.mostClassify();
    }

    /*
    * 获取最多借阅次数的用户名
    * */
    @Override
    public Object mostUser() {
        return BorrowMapper.mostUser();
    }
}
